package com.sk.ccflower.lib.commonUtils;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;

import org.apache.log4j.Logger;

public class ConfigUtil{

    private Logger log;

    private static ConfigUtil me=new ConfigUtil();

    private ConfigUtil(){
        log=Logger.getLogger( getClass() );

    }

    public static ConfigUtil getInstance(){
        return me;
    }


    /**
     * 프러퍼티 파일의 내용을 로드한다.
     * @param path String 절대경로 / 상대경로 모두 사용해도 무방하다.
     * @return Properties
     */
    public Properties getProperties( String path){
        Properties pty=new Properties();
        InputStream input=null;
        try{
            input=new FileInputStream( new File( path ) );
            pty.load( input );
        } catch( FileNotFoundException ex ){
            log.error( ex.toString() , ex );
        } catch( IOException ex ){
            log.error( ex.toString() , ex );
        } finally{
            if( input!=null ){
                try{
                    input.close();
                } catch( IOException ioe ){
                    log.error( ioe.toString() , ioe );
                }
            }

        }
        return pty;
    }


    /**
     *
     * @param cls Class CFG 파일과 매핑할 클래스 자료구조를 나타냄.
     * @param cfgFileName String CFG 파일경로 ( 절대경로 , 상대경로 모두 사용가능 )
     * @return Object
     * @throws Exception
     */
    public Object readAllProperty( Class<?> cls , String cfgFileName ) throws Exception{
        Object obj=null;
        Properties pty= ConfigUtil.getInstance().getProperties(cfgFileName);
        Field[] fields=null;
        String cfgKey=null;
        try{

            fields=cls.getFields();
            for( Enumeration eKey=pty.keys(); eKey.hasMoreElements(); ){
                cfgKey=( String )eKey.nextElement();
                for( int i=0; i<fields.length; i++ ){

                    if( fields[i].getName().equals( cfgKey ) ){

                        if( fields[i].getType().isPrimitive() ){
                            fields[i].setInt( new Integer( ( String )pty.get( cfgKey ) ) , Integer.parseInt( ( String )pty.get( cfgKey ) ) );
                        } else if( fields[i].getType().isInstance( new String() ) ){
                            fields[i].set( ( String )pty.get( cfgKey ) , ( String )pty.get( cfgKey ) );
                        }
                        //log.debug( "KEY="+fields[i].getName()+" , VALUE="+( String )pty.get( cfgKey ) );
                    }
                }

            }
            obj=cls.newInstance();

        } catch( Exception e ){
            log.error( e.toString() , e );
            throw e;
        }
        return obj;

    }

    public void writeAllProperty( HashMap<String , String> hash , String path){
        Properties pty = new Properties() ;
        pty.putAll(hash);
        this.writeAllProperty(pty , path);
    }

    public void writeAllProperty( Properties pty , String path){

        File f =  new File( path );
        OutputStream os = null;
        try{
            new FileUtil().newFile(path);
            os=new FileOutputStream( f );
            pty.store(os , "## Do not Edit ##");
        } catch( FileNotFoundException ex1 ){
            this.log.error(ex1.toString() ,ex1);
        } catch( IOException ex){
            this.log.error(ex.toString() ,ex);
        } finally{
            if( os != null){
                try{
                    os.close();
                } catch( IOException ex2 ){
                }
            }
        }


    }
    public void changeCopyProperties( File ori , File dest , HashMap<String , String> att ) throws Exception{
        Properties pty=new Properties();

        InputStream input=null;
        OutputStream output=null;

        try{

            input=new FileInputStream( ori );
            output=new FileOutputStream( dest );
            pty.load( input );
            String key=null;
            Iterator it = att.keySet().iterator();
            while( it.hasNext()){

                key=( String ) it.next();
                pty.setProperty( key , ( String )att.get( key ) );
            }

            pty.store( output , "### Do not edit ###" );

        } catch( FileNotFoundException ex ){
            throw ex;
        } finally{
            if( input!=null ){
                input.close();
            }
            if( output!=null ){
                output.close();
            }
        }
    }
}
